package com.attendance.sz.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016-04-27.
 */
public class SharePreferenceUtil {
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    public SharePreferenceUtil(Context context, String file) {
        sp = context.getSharedPreferences(file, context.MODE_PRIVATE);
        editor = sp.edit();
    }


    public boolean isFirstRun() {
        return sp.getBoolean("isFirstRun", true);
    }

    // 是否登录
    public void setIsLogin(boolean isLogin) {
        editor.putBoolean("isLogin", isLogin);
        editor.commit();
    }

    public boolean getIsLogin() {
        return sp.getBoolean("isLogin", false);
    }



    public String getMachineId() {
        return sp.getString("MachineId", "0");
    }

    public void setFig(Boolean fig) {
        editor.putBoolean("fig", fig).commit();
    }


    public Boolean getFig() {
        return sp.getBoolean("fig", true);
    }
    public void setDay(int day) {
        editor.putInt("day", day).commit();
    }


    public int getDay() {
        return sp.getInt("day", 0);

}
    /**
     * 存储List<String>
     *
     * @param context
     * @param key
     *            List<String>对应的key
     * @param strList
     *            对应需要存储的List<String>
     */
    public  void putStrListValue(Context context, String key,
                                       List<String> strList) {
        if (null == strList) {
            return;
        }
        // 保存之前先清理已经存在的数据，保证数据的唯一性
        int size = strList.size();
        putIntValue(context, key + "size", size);
        for (int i = 0; i < size; i++) {
            putStringValue(context, key + i, strList.get(i));
        }
    }


    public  void putIntValue(Context context, String key, int value) {
        editor.putInt(key, value).commit();


    }
    public  void remove( String key) {
        editor.remove(key).commit();


    }

    /**
            * 存储数据(String)
    *
            * @param context
    * @param key
    * @param value
    */
    private  void putStringValue(Context context, String key, String value) {
        editor.putString(key, value).commit();
    }
    /**
     * 取出数据（int)
     *
     * @param context
     * @param key
     * @param defValue
     *            默认值
     * @return
     */
    private  int getIntValue(Context context, String key, int defValue) {
        int value = sp.getInt(key, defValue);
        return value;
    }
    /**
     * 取出数据（String)
     *
     * @param context
     * @param key
     * @param defValue
     *            默认值
     * @return
     */
    private  String getStringValue(Context context, String key,
                                         String defValue) {
        String value = sp.getString(key, defValue);
        return value;
    }
    /**
     * 取出List<String>
     *
     * @param context
     * @param key
     *            List<String> 对应的key
     * @return List<String>
     */
    public  List<String> getStrListValue(Context context, String key) {
        List<String> strList = new ArrayList<String>();
        int size = getIntValue(context, key + "size", 0);
        //Log.d("sp", "" + size);
        for (int i = 0; i < size; i++) {
            strList.add(getStringValue(context, key + i, null));
        }
        return strList;
    }

}